package com.timothy.junk.calculate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpException;

import android.util.Log;

import com.sina.oefund.SinaOEFund;
import com.sina.oefund.SinaOEFundClient;
import com.sina.stock.SinaStockClient;
import com.sina.stock.SinaStockInfo;
import com.sina.stock.SinaStockInfo.ParseStockInfoException;
import com.timothy.junk.common.util.DateUtil;
import com.timothy.junk.tfund.bean.TFund;
import com.timothy.junk.tfund.bean.TFundResult;

public class TFundCalculator implements Calculator{
	public static final String LOG_TAG = "TFundCalculator";
	
    private List<TFund> tFundList;
    private List<TFundResult> tFundResultList = new ArrayList<TFundResult>() ;
    private int count = 0;
	
	public TFundCalculator(List<TFund> tFundList) {
		super();
		this.tFundList = tFundList;
		count = tFundList.size();
	}
	
	//input data
    String[] tFundInCodes;
	String[] tFundOutCodes;
	List<SinaStockInfo> tFundStockList =  new ArrayList<SinaStockInfo>();
	List<SinaOEFund> oeFundList =  new ArrayList<SinaOEFund>();

	@Override
	public void initial() {
		
		tFundInCodes = new String[count];
		tFundOutCodes = new String[count];
		
		int i = 0;
		for (TFund tFund : tFundList) {
			String inCode = tFund.getTfundCode();
			tFundInCodes[i] = inCode;
			String outCode = inCode.replace("sh", "jj").replace("sz", "jj");
			tFundOutCodes[i] = outCode;
			i++;
		}
	}
	
	public void getPriceList(SinaStockClient sClient){
		try {
			tFundStockList = sClient.getStockInfoList(tFundInCodes);
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseStockInfoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getValueList(SinaOEFundClient fClient){
		try {
			oeFundList = fClient.getFundList(tFundOutCodes);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (com.sina.oefund.SinaOEFund.ParseStockInfoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void calculate() {
		
		int i=0;
		for (TFund tFund : tFundList) {
			TFundResult tFundResult = new TFundResult();
			tFundResult.setSeq(tFund.getSeq());
			tFundResult.setTfundCode(tFund.getTfundCode());
			tFundResult.setTfundName(tFund.getTfundName());
			tFundResult.setEndDate(tFund.getEndDate());
			
			SinaStockInfo ssInfo = tFundStockList.get(i);
			SinaOEFund oeFund = oeFundList.get(i);
			
			double tfundPrice = Double.valueOf(ssInfo.getNowPrice());
			double tfundValue = oeFund.getPrice();
			String tfundValueDate = oeFund.getPriceDate();
			double discount = (tfundValue - tfundPrice)/tfundValue;
			
			
			String nowDate = DateUtil.getNowDate();
			String endDate = tFund.getEndDate();
			int rDays = DateUtil.getDayBetween(nowDate, endDate).intValue();
			double rYears = rDays/365.0; 
			double tReturn = discount/(1 - discount);
			
			tFundResult.setTfundPrice(tfundPrice);
			tFundResult.setTfundValue(tfundValue);
			tFundResult.setTfundValueDate(tfundValueDate);
			tFundResult.setDiscount(discount);
			tFundResult.setTotalReturn(tReturn);
			tFundResult.setRemainingDays(rDays);
			tFundResult.setYearReturn(tReturn/rYears);
			
			Log.i(LOG_TAG,tFundResult.toString());
			
			tFundResultList.add(tFundResult);
			
			i++;
		}
		
		
	}

	public List<TFundResult> gettFundResultList() {
		return tFundResultList;
	}

}
